import 'package:flutter/cupertino.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:native_dart_package/http/request/exception/exception_handler.dart';
import 'package:native_dart_package/loading/loading_util.dart';
import 'package:yt_devops/components/string_extension.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/services/auth/auth_services.dart';
import 'package:yt_devops/utils/func_utils.dart';

class MineResetPasswordController extends GetxController {
  final count = 0.obs;
  TextEditingController pwdController = TextEditingController();
  TextEditingController againPwdController = TextEditingController();
  bool enableCommit = false;
  final _authServices = Get.put(AuthServices());
  late num userId;
  @override
  void onInit() {
    super.onInit();
    if (Get.arguments != null) {
      userId = Get.arguments;
    }
  }

  @override
  void onReady() {}
  @override
  void onClose() {}
  textOnchange(String text) {
    if (pwdController.text.isNotEmpty &&
        againPwdController.text.isNotEmpty &&
        text.isNotEmpty) {
      enableCommit = true;
    } else {
      enableCommit = false;
    }
    update();
  }

  Future<void> save() async {
    if (pwdController.text != againPwdController.text) {
      EasyLoading.showError(ResKey.mineUserManagerPwdInconsistency.tr);
      return;
    }
    if (!pwdController.text.tyIsPassword) {
      EasyLoading.showError(ResKey.mineUserManagerPwdLimit.tr);
      return;
    }
    var param = {
      "userId": userId,
      "password": pwdController.text,
      "confirmPassword": againPwdController.text,
    };
    showLoading(toast: ResKey.loading.tr);
    var result = await _authServices.resetUserPwd(param, onError: (error) {
      dismissLoading();
      handleException(error);
      return false;
    });
    dismissLoading();
    // if(result != null){
    FuncUtils.requestTipsDislog(
        Get.context, ResKey.mineUserManagerPwdResetSuccess.tr, true,
        duration: const Duration(milliseconds: 1000));

    /// 需要做个延迟加载不然提示框无法显示
    Future.delayed(const Duration(milliseconds: 1000), () {
      // 延迟执行的代码
      Get.back();
    });
    // }
  }
}
